//
//  HelloWorldLayer.m
//  NewYearRun
//
//  Created by Lion User on 09/01/2012.
//  Copyright __MyCompanyName__ 2012. All rights reserved.
//


// Import the interfaces
#import "MainLayer.h"
#import "CCAnimationHelper.h"
// HelloWorldLayer implementation
@implementation MainLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	MainLayer *layer = [MainLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		// add touch enabled
        self.isTouchEnabled = YES;
        // init param
        persons = [[NSMutableArray alloc] init];
        vehicles = [[NSMutableArray alloc] init];
        //add backgroud picture
        CCSprite *bg = [CCSprite spriteWithFile: @"bgPic.png"]; 
        bg.position = ccp(0,0);
        [self addChild:bg];
        // play background music
        // TODO
        //[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background-music.caf"];
        // person logic
        [self schedule:@selector(gamePersonLogic:) interval:2.0];
        // vehicle logic
        [self schedule:@selector(gameBusLogic:) interval:3.0];
        [self schedule:@selector(gameTrainLogic:) interval:5.0];
        [self schedule:@selector(gamePlaneLogic:) interval:5.0];
        // update game page
        [self schedule:@selector(update:)];
	}
	return self;
}
// person logic, add or set bus depart,set flag only,redraw at update function
-(void)gamePersonLogic:(ccTime)dt {
    
}
// bus logic, add or set bus depart,set flag only,redraw at update function
-(void)gameBusLogic:(ccTime)dt {
    //利用文件名创建动作    
    //--首先导入#import "CCAnimationHelper.h"   
    CCSprite*mySprite=[CCSprite spriteWithFile:@"bus0.png"];
    mySprite.position=ccp(140,mySprite.contentSize.height*0.5);   
    [self addChild:mySprite];   
    
    CCAnimation*anim=[CCAnimation animationWithFile:@"bus" frameCount:12 delay:0.1];
    CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
    CCSequence *seq = [CCSequence actions:animate,nil];
    CCRepeatForever* repeat = [CCRepeatForever actionWithAction:seq];
    [mySprite runAction:repeat];
    // 上面这种播放动画，在精灵多的时候效率不高。
    // 更多的时候使用楨来播放动画。
}
// train logic, add or set bus depart,set flag only,redraw at update function
-(void)gameTrainLogic:(ccTime)dt {
    
}
// plane logic, add or set bus depart,set flag only,redraw at update function
-(void)gamePlaneLogic:(ccTime)dt {
    
}
// if self.isTouchEnabled = YES; this function will be called when touch
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
}
// update game persons,vehicles and others at this function
- (void)update:(ccTime)dt {
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
